﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BatchRenameFilesForFtpUse
{
    class Program
    {
        // variables
        string srcFileName;
        string destFileName;
        String[] files;


        // Rename file names in directory given as argument
        static void Main(string[] args)
        {

            // Command line parameters snippet
            //Console.WriteLine("Number of command line parameters = {0}", args.Length);
            //            for (int i = 0; i < args.Length; i++)
            //{
            ///Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
            //}


            // There is no argument issue
            if (args.Length < 1)
            {
                System.Console.WriteLine("USAGE:");
                System.Console.WriteLine("batchrenamefilesforftpuse.exe [DIRECTORY_PATH]");
                System.Console.WriteLine("     -> to perform on specific path");
                System.Console.WriteLine("OR:");
                System.Console.WriteLine("batchrenamefilesforftpuse.exe .");
                System.Console.WriteLine("     -> to perform on current directory");
                System.Console.WriteLine();
                System.Console.WriteLine("ERROR: No argument were given.");
                Environment.Exit(1);
            }


            try
            {
                // Program START
                Program p = new Program();
                p.rename(args[0].ToString());

                System.Console.WriteLine("DONE");

            }
            catch (Exception x) // The argument is not directory path or there is another reason
            {
                System.Console.WriteLine("USAGE:");
                System.Console.WriteLine("batchrenamefilesforftpuse.exe [DIRECTORY_PATH]");
                System.Console.WriteLine("     -> to perform on specific path");
                System.Console.WriteLine("OR:");
                System.Console.WriteLine("batchrenamefilesforftpuse.exe .");
                System.Console.WriteLine("     -> to perform on current directory");
                System.Console.WriteLine();
                System.Console.WriteLine("POSSIBLE ERROR: Argument is not directory path! It should looks like: " + @"C:\MyDirectory");
                System.Console.WriteLine();
                System.Console.WriteLine("SYSTEM ERROR MESSAGE:");
                System.Console.WriteLine(x.Message);
            }


        }



        // Rename filenames
        private void rename(String directory)
        {

            files = Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories); // only files


            foreach (string item in files)
            {
                srcFileName = Path.GetFileName(item);
                destFileName = srcFileName;
                destFileName = destFileName.ToLower();
                destFileName = destFileName.Replace(" ", "_");
                destFileName = replaceLanguageSpecific(destFileName);
                string newItem = item.Replace(srcFileName, destFileName);
                File.Move(item, newItem);
                Console.Write(".");
            }
        }

        // Replace language specific characters
        private string replaceLanguageSpecific(string destFileName)
        {
            StringBuilder sb = new StringBuilder(destFileName);

            // polish specific characters
            sb.Replace("ą", "a")
            .Replace("ę", "e")
            .Replace("ł", "l")
            .Replace("ż", "z")
            .Replace("ź", "z")
            .Replace("ć", "c")
            .Replace("ń", "n")
            .Replace("ó", "o")
            .Replace("ś", "s");
            return sb.ToString();
        }


    }

}
